package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mine.site.dao.MessageDao;
import com.mine.site.model.Message;


/**
 * Servlet implementation class LiuyanServlet
 */
@WebServlet("/LiuyanServlet")
public class LiuyanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LiuyanServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		HttpSession session = request.getSession();

		ServletContext ctx = request.getServletContext();

		PrintWriter out =  response.getWriter();
		
		String method = request.getParameter("m");
		if("del".equals(method)){
			this.del(request, response);
		}else if("forEdit".equals(method)){
			this.beforeEdit(request, response);
		}else if("update".equals(method)){
			this.update(request, response);
		}else if("save".equals(method)){
			this.save(request, response);
		}else if("find".equals(method)){
			this.find(request, response);
		}else if("byname".equals(method)){
			this.bynamme(request, response);
		}
		else{
			this.list(request, response);
		}
	}

	private void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessageDao dao = new MessageDao();
		List<Message> liuyan = dao.findAll();
		request.setAttribute("messageList", liuyan);
		
		request.getRequestDispatcher("bsmessage.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessageDao dao = new MessageDao();
		List<Message> liuyan = dao.findAll();
		request.setAttribute("messageList", liuyan);
		
		request.getRequestDispatcher("message.jsp").forward(request, response);
		
	}

	private void bynamme(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Message u = wrapUser(request);
		String id=UUID.randomUUID().toString().replaceAll("-", ""); 
		u.setId(id);
		MessageDao dao = new MessageDao();
		dao.save(u);
		response.sendRedirect("LiuyanServlet");
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		Message u = wrapUser(request);
		MessageDao dao = new MessageDao();
		dao.update(u);
		response.sendRedirect("LiuyanServlet");
	}

	private void beforeEdit(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String Id = request.getParameter("Id");
		MessageDao dao = new MessageDao();
		dao.delete(Id);
		request.getRequestDispatcher("LiuyanServlet?m=find").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	@SuppressWarnings("unused")
	private Message wrapUser(HttpServletRequest request) {
		Message u = new Message();	
		u.setName(request.getParameter("Name"));
		u.setMessage(request.getParameter("Message"));
		u.setId(request.getParameter("Id"));
		return u;
	}
}
